﻿using UnityEngine;
using System.Collections;


namespace UI
{
    public class EquipPosition : MonoBehaviour
    {
        public GameObject nameBg;
        public UILabel equipName;
        public int posIndex;
        public string equipGuid;

        public EquipIcon M_EquipIcon
        {
            get
            {
                if (equipIcon != null)
                    return equipIcon.GetComponent<EquipIcon>();
                else
                    return null;
            }
        }

        GameObject equipIcon = null;

        void Awake()
        {
            nameBg.SetActive(false);
        }

        public void SetEquip(UIEquipment equip)
        {
            nameBg.SetActive(true);
            equipName.text = equip.GetWeaponNameWithHero(equip.lastHeroId); //TableTextBlo.GetText("name_" + equip.equipId).Replace("\\r", "\r").Replace("\\n", "\n");
            equipGuid = equip.guid;

            if(equipIcon == null)
            {
                GameObject obj = AssetLoad.ResourceManager.Instance.LoadResource("prefab/EquipIcon") as GameObject;
                equipIcon = Instantiate(obj);
                equipIcon.transform.parent = this.gameObject.transform;
                equipIcon.transform.localPosition = new Vector3(0, 0, 0);
                equipIcon.transform.localScale = new Vector3(1, 1, 1);
                Destroy(equipIcon.GetComponent<BoxCollider>()); 
            }
            equipIcon.GetComponent<EquipIcon>().SetEquip(equip);
        }

        public void SetTreasure(UITreasure treasure)
        {
            nameBg.SetActive(true);
            equipName.text = TableTextBlo.GetText("name_" + treasure.treasureId).Replace("\\r", "\r").Replace("\\n", "\n");
            equipGuid = treasure.guid;

            if (equipIcon == null)
            {
                GameObject obj = AssetLoad.ResourceManager.Instance.LoadResource("prefab/EquipIcon") as GameObject;
                equipIcon = Instantiate(obj);
                equipIcon.transform.parent = this.gameObject.transform;
                equipIcon.transform.localPosition = new Vector3(0, 0, 0);
                equipIcon.transform.localScale = new Vector3(1, 1, 1);
                Destroy(equipIcon.GetComponent<BoxCollider>());
            }
            equipIcon.GetComponent<EquipIcon>().SetTreasure(treasure);
        }

        public void ClearPosition()
        {
            nameBg.SetActive(false);
            Destroy(equipIcon);
            equipIcon = null;
            equipGuid = null;
        }
    }

}
